<template>
  <div class="table">

    <div style="margin: 10px 0;padding: 0 5px;display: flex">
      <el-popconfirm style="padding-left: 5px;padding-right: 5px" title="确认批量删除吗?" @confirm="delBatch">
        <template #reference>
          <el-button type="danger">批量删除</el-button>
        </template>
      </el-popconfirm>
      <el-button plain icon="el-icon-left" style="margin-left: auto;margin-right: 40px" @click="()=>$router.back()">返回
      </el-button>
    </div>


    <!--显示乐谱-->
    <el-table :data="tableData" stripe style="width: 100%;" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="序号" width="55" v-if="false"></el-table-column>
      <el-table-column prop="name" label="乐谱名" width="120"></el-table-column>
      <el-table-column prop="introduction" label="介绍" align="center">
        <template slot-scope="scope" width="120%">
          <p style="height: 60px;line-height: 1.4em">{{ scope.row.introduction }}</p>
        </template>
      </el-table-column>
      <el-table-column prop="picture" label="封面" align="center">
        <template slot-scope="scope">
          <div>
            <img :src="scope.row.picture" alt="" style="width: 50px;height: 50px">
          </div>
          <el-upload ref="upload" :action="uploadUrl(scope.row.id)" :on-success="filesUploadSuccess">
            <el-button size="mini">更新图片</el-button>
          </el-upload>
        </template>
      </el-table-column>
      <el-table-column prop="staff" label="评论" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="preViewComment(scope.row.id)">评论显示</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="staff" label="乐谱" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="previewXml(scope.row.file)">乐谱显示</el-button>
        </template>
      </el-table-column>
      <!--按钮 -->
      <el-table-column label="操作" width="180px">
        <template slot-scope="scope">
          <el-popconfirm title="这是一段内容确定删除吗？" @confirm="handleDelete(scope.row.id)">
            <el-button slot="reference" type="danger" style="margin-left: 6px" icon="el-icon-delete"></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div style="padding: 10px 0;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

  </div>
</template>

<script>

import {deleteCollect, deleteCollectLists, getCollectOfStaff,} from "../api/index";
import {mixin} from "@/mixins";
import showXml from '../util/showxml'


export default {
  mixins: [mixin],
  name: "Collect",
  data() {
    return {
      consumerId: '',//上传者id
      //查询
      tableData: [],
      total: 0,//分页，默认总数为0
      pageNum: 1,//分页，默认第一页
      pageSize: 5,//分页，默认一页有两条
      editVisible: false,//默认不展示
      multipleSelection: [],//批量删除选中集合

    }
  },

  created() {
    this.consumerId = this.$route.query.id;
    this.getData();
  },
  //更新图片
  methods: {
    uploadUrl(id) {
      return `http://localhost:8888/staff/upload?id=${id}`
    },
    //图片更新成功之后
    filesUploadSuccess() {
      this.getData()//刷新表格
    },
    //根据指定用户id查询收藏列表
    getData() {
      this.tableData = [];//清空tableData里的数据
      getCollectOfStaff(
        this.pageNum,
        this.pageSize,
        this.consumerId,
        this.name
      ).then(res => {
        const {records, total, pages} = res.data;
        this.tableData = records
        this.total = total
        if (this.pageNum > 1 && pages < this.pageNum) {
          console.log('当前页没有数据并且不是第一页是，重新请求前一页的数据')
          this.pageNum--;
          this.getData();
        }
      })
    },

    // 处理搜索按钮点击
    handleQuery() {
      this.pageNum = 1;
      this.getData();
    },
    //分页
    handleSizeChange(pageSize) {
      console.log(`每页 ${pageSize} 条`);
      this.pageSize = pageSize
      this.getData()//调用分页方法
    },
    //分页
    handleCurrentChange(pageNum) {
      console.log(`当前页: ${pageNum}`);
      this.pageNum = pageNum
      this.getData()//调用分页方法
    },

    //删除
    handleDelete(id) {
      deleteCollect(id).then(res => {
        if (res) {
          this.$message.success("删除成功")
          this.getData()
        } else {
          this.$message.success("删除失败")
        }
      })
    },
    //批量删除
    handleSelectionChange(val) {
      console.log("val=" + val)
      this.multipleSelection = val
    },
    //批量删除按钮
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)//把对象数组变成集合[{},{},{}] ----> [1,2,3]
      deleteCollectLists(ids).then(res => {
        console.log('ids=' + ids)
        if (res.code === 200) {
          this.$message.success("批量删除成功")
          this.getData()
        } else {
          this.$message.success("批量删除失败")
        }
      })
    },
    //查看乐谱
    previewXml(fileUrl) {
      // 这里传入不同的url，展示不同滴文件
      showXml(fileUrl)
    },
    //转向该用户的收藏列表
    preViewComment(id) {
      this.$router.push({path: '/comment', query: {id}})
    },

  }
}
</script>

<style scoped>


</style>
